package gr.handmademarket.ui.pages;

import gr.handmademarket.bl.operations.LoadCategoriesOperation;
import gr.handmademarket.po.implementations.ProductCategory;
import gr.handmademarket.po.interfaces.Category;
import gr.handmademarket.ui.tabs.CategoryTab;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.extensions.ajax.markup.html.tabs.AjaxTabbedPanel;
import org.apache.wicket.extensions.markup.html.tabs.ITab;
import org.apache.wicket.markup.html.image.NonCachingImage;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.ContextRelativeResource;

/**
 * home page of our application.
 */
public class HomePage 
extends CommonTemplatePage{
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * tabs list.
	 */
	private List<ITab> tabs = createTabs();
	
	/**
	 * the tabbed panel.
	 */
	@SuppressWarnings({ "nls", "rawtypes", "unchecked" })
	private AjaxTabbedPanel tabbedPanel = new AjaxTabbedPanel("tabbedPanel", tabs);
	
	/**
	 * 
	 * Creates a new HomePage object. 
	 *
	 */
	public HomePage(){
		super();
		add(tabbedPanel);
		add(createCarousel());
	}
	
	/**
	 * @return list of tabs.
	 */
	private List<ITab> createTabs(){
		List<ITab> itabs = new ArrayList<ITab>();
		LoadCategoriesOperation op = new LoadCategoriesOperation();
		op.execute();
		List<ProductCategory> categories = op.getCategories();
		for (Category category : categories){
			CategoryTab tab = new CategoryTab(new Model<Category>(category));
			itabs.add(tab);
		}
		return itabs;
	}
	
	private ListView<NonCachingImage> createCarousel(){
		ListView<NonCachingImage> imagesView = null;
		String path = "/images/creators/profile_pics";
		List<NonCachingImage> imgList = new ArrayList<NonCachingImage>();
		for (int i = 6 ; i < 8; i++){
			ContextRelativeResource resource = new ContextRelativeResource(path+"/"+i+".jpg");
			NonCachingImage image = new NonCachingImage("img", resource);
			imgList.add(image);
		}
		imagesView = new ListView<NonCachingImage>("lview", imgList){

			/**
			 * serialVersionUID
			 */
			private static final long serialVersionUID = 1L;
			
			private int cnt = 0;

			@Override
			protected void populateItem(ListItem<NonCachingImage> item) {
				item.add(item.getModelObject());
				if (cnt == 0){
					item.add(AttributeModifier.append("class", "active"));
				}
				cnt++;
			}
			
		};
		return imagesView;
	}
	
}
